package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;

import io.netty.channel.*;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyServer;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket.NettySocksClientProxyServerSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocketClientProxyServerStartTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @see ProxyMessageType#SOCKS_CLIENT_PROXY_SERVER_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ClientHandleNettySocketProtocolHandleSocketClientProxyServerStartTypeAdvanced
        extends AbstractHandleSocketClientProxyServerStartTypeAdvanced<NettyProxyMsg> {
    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {


        NettySocketChannelContext nettySocketChannelContext = (NettySocketChannelContext) nettyChannelContext;
        Channel proxyChannel = nettySocketChannelContext.channel();
        String host = nettyProxyMsg.getTargetIpString();
        Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
        Socks5AddressType socks5AddressType = nettySocketChannelContext.getSocks5AddressType();


        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);


        NettySocksClientProxyServer nettySocksClientProxyServer = new NettySocksClientProxyServer();
        nettySocksClientProxyServer.setSsl(false);
        nettySocksClientProxyServer.setTargetIp(host);
        nettySocksClientProxyServer.setTargetPort(Integer.parseInt(String.valueOf(port)));
        nettySocksClientProxyServer.setSocks5AddressType(socks5AddressType);


        nettySocksClientProxyServer.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
        nettySocksClientProxyServer.setNettyClientProperties(nettyClientProperties);

        // 创建连接
        NettySocksClientProxyServerSocket.buildTransferServer(nettySocksClientProxyServer, proxyChannel);



    }
}
